/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uniq.servlet;

import com.uniq.db.ConnectionFactory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import javax.servlet.annotation.WebServlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
@WebServlet("/DisplayImage")
public class DisplayImage extends HttpServlet {
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException
{
    // Code to access database and get blob image.
             String id =request.getParameter("id");
            // select from table where id='id'
             Blob photo=null;
            String filePath=getServletContext().getRealPath("/images");
            
        //    File file=new File(filePath);
            
            Connection con=ConnectionFactory.getConnection();
            
            Statement ss=con.createStatement();
            
            String query="Select * from stock where id='"+id+"'";
            
            ResultSet rs=ss.executeQuery(query);
            if(rs.next())
            {
             photo=rs.getBlob("product_image");

        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType("image/jpeg");
        response.setHeader("Content-Length", String.valueOf(photo.length()));

        // Prepare streams.
            }
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        // Open streams.
        input = new BufferedInputStream(photo.getBinaryStream(), DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally {            
        output.close();
        input.close();
    }                           

        }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        try {
            processRequest(req, resp);
        } catch (SQLException ex) {
            Logger.getLogger(DisplayImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
     try {
            processRequest(req, resp);
        } catch (SQLException ex) {
            Logger.getLogger(DisplayImage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
}